<template>
  <div class="profile-box">
    <!-- 未登录头部 -->
    <div v-if="showTop" class="not-login login-bg">
      <img src="@/assets/mobile.png" alt="" @click="$router.push('/login')">
      <span>登录 / 注册</span>
    </div>
    <!-- 已登录头部 -->
    <div v-else class="login login-bg">
      <div class="user_info">
        <div class="user_container">
          <div class="left">
            <van-image class="avatar" round :src="userInfo.photo" />
            <span class="user_name">{{userInfo.name}}</span>
          </div>
          <van-button type="default" size="mini" round to="/user">编辑资料</van-button>
        </div>
      </div>
      <div class="user_data">
        <div class="data_item">
          <span>{{userInfo.art_count}}</span>
          <span>头条</span>
        </div>
        <div class="data_item">
          <span>{{userInfo.follow_count}}</span>
          <span>关注</span>
        </div>
        <div class="data_item">
          <span>{{userInfo.fans_count}}</span>
          <span>粉丝</span>
        </div>
        <div class="data_item">
          <span>{{userInfo.like_count}}</span>
          <span>获赞</span>
        </div>
      </div>
    </div>
    <!-- 宫格导航 -->
    <van-grid class="nav-grid" :column-num="2">
      <van-grid-item>
        <i slot="icon" class="toutiao toutiao-shoucang"></i>
        <span slot="text" class="grid_text">收 藏</span>
      </van-grid-item>
      <van-grid-item>
        <i slot="icon" class="toutiao toutiao-lishi"></i>
        <span slot="text" class="grid_text">历 史</span>
      </van-grid-item>
    </van-grid>
    <!-- cell单元格 -->
    <div class="cell-box">
      <van-cell title="消息通知" is-link />
      <van-cell title="小智同学" is-link />
    </div>
    <van-cell v-if="!showTop" title="退出登录" class="cell-logout" @click="exitLogin" />
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getUserInfo } from '@/api/user.js'
export default {
  name: 'profile',
  data () {
    return {
      // 用户信息
      userInfo: {}
    }
  },
  methods: {
    // 退出登录
    exitLogin () {
      this.$dialog.confirm({
        title: '头条',
        message: '确认退出'
      })
        .then(() => {
          this.$store.commit('initUser', null)
        })
        .catch(() => {
          // on cancel
        })
    },
    // 加载用户信息
    async loadUserInfo () {
      try {
        const { data: res } = await getUserInfo()
        console.log(res.data)
        this.userInfo = res.data
      } catch (err) {
        this.$toast('获取用户信息失败')
      }
    }
  },
  computed: {
    // 判断登录状态
    showTop () {
      return !this.user
    },
    ...mapState(['user'])
  },
  watch: {},
  filters: {},
  components: {},
  created () {
    // 只有登录状态才加载用户信息
    if (this.user) {
      this.loadUserInfo()
    }
  },
  mounted () { }
}
</script>

<style scoped lang='less'>
// 公共背景
.login-bg {
  height: 400px;
  background-image: url(~@/assets/banner.png);
  background-size: 100%;
}
.profile-box {
  // 未登录
  .not-login {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    img {
      width: 132px;
      height: 132px;
      margin-bottom: 15px;
    }
    span {
      font-size: 28px;
      color: #fff;
    }
  }
  // 已登录
  .login {
    .user_info {
      display: flex;
      align-items: flex-end;
      padding: 0 32px;
      height: 247px;
      .user_container {
        width: 100%;
        height: 132px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        .left {
          display: flex;
          align-items: center;
          .avatar {
            width: 132px;
            height: 132px;
          }
          .user_name {
            font-size: 30px;
            color: #fff;
            margin-left: 22px;
          }
        }
      }
    }
    .user_data {
      display: flex;
      justify-content: space-around;
      height: 153px;
      .data_item {
        display: flex;
        align-items: center;
        justify-content: center;
        flex-direction: column;
        color: #fff;
        span:nth-child(1) {
          margin-bottom: 20px;
          font-size: 26px;
        }
        span:nth-child(2) {
          font-size: 22px;
        }
      }
    }
  }
  // 宫格导航
  .nav-grid {
    i {
      font-size: 45px;
    }
    .toutiao-shoucang {
      color: #eb5253;
    }
    .toutiao-lishi {
      color: #ff9d1d;
    }
    .grid_text {
      margin-top: 16px;
      font-size: 27px;
      color: #333;
    }
  }
  .cell-box {
    margin: 10px 0;
  }
  .cell-logout {
    text-align: center;
    color: #d86263;
  }
}
</style>
